package com.anos.demoinserticon;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 4/1/2014.
 */
public class EmoticonsMgr {

    private static final HashMap<String, Integer> mEmoticons = new HashMap();

    static {
        mEmoticons.put(":*", R.drawable.sms_01);
        mEmoticons.put(":-D", R.drawable.sms_01);
        mEmoticons.put(";)", R.drawable.sms_01);
        mEmoticons.put(":-(", R.drawable.sms_01);
    }

    public static Spannable getSmiledText(Context context, String text) {

        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        int index;

        for (index = 0; index < builder.length(); index++) {
            for (Map.Entry<String, Integer> entry : mEmoticons.entrySet()) {
                int length = entry.getKey().length();
                if (index + length > builder.length())
                    continue;
                if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
                    builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    index += length - 1;
                    break;
                }
            }
        }
        return builder;
    }
}
